jQuery.noConflict();
jQuery(function($){
	$(".true").hide();
	$(":input.required").blur(function(event){
		//判断一下鼠标离开谁了
		//验证用户名
		if($(this).is(".username")){
			if($(".username").val()==""){
				var errMsg = "<div class='true'>用户名不能为空！</div>";
				$(".username_tishi").html(errMsg);
				return false;
			}else{
				if(this.value.length<3){
					var errMsg = "<div class='true'>用户名不能少于3位！</div>";
					$(".username_tishi").html(errMsg);
					return false;
				}else{
					if (rcheckUniquename())
					{
						var Msg = "<div class='true'>用户名输入正确！</div>";
						$(".username_tishi").html(Msg);
						return true; 
					}else{
						var errMsg = "<div class='true'>该用户名已被注册！</div>";
						$(".username_tishi").html(errMsg);
						return false;						
					}					
				}
			}
		}
		
		//验证邮箱
		if($(this).is(".email")){
			var email=$(".email").val();
			if($(".email").val()==""){
				var errMsg = "<div class='true'>邮箱不能为空！</div>";
				$(".emali_tishi").html(errMsg);
				return false;
			}else{
				if(!valid_email(email)){
					var errMsg = "<div class='true'>邮箱不合法！</div>";
					$(".emali_tishi").html(errMsg);
					return false;
				}else{
					if (rcheckUniqueemail())
					{
						var Msg = "<div class='true'>>邮箱可以使用！</div>";
						$(".emali_tishi").html(Msg);
						return true;
					}else{						
						var Msg = "<div class='true'>该邮箱已被注册！</div>";
						$(".emali_tishi").html(Msg);
						return false;
					}
					
				}
			}
		}
		
		//验证密码
		if($(this).is(".password")){
			if($(".password").val() == ""){
				var errMsg = "<div class='true'>密码不能为空！</div>";
				$(".password_tishi").html(errMsg);
				return false;
			}else{
				if($(".password").val().length<6){
					var errMsg = "<div class='true'>密码不能小于6位！</div>";
					$(".password_tishi").html(errMsg);
					return false;
				}else{
					var Msg = "<div class='true'>密码输入正确</div>";
					$(".password_tishi").html(Msg);
					return true;
				}
			}
		}
		
		//验证重复密码
		if($(this).is(".password1")){
			if($(".password1").val()==""){
				var errMsg = "<div class='true'>确认密码不能为空！</div>";
				$(".password1_tishi").html(errMsg);
				return false;
			}else{
				if($(".password1").val()!==$(".password").val()){
					var errMsg = "<div class='true'>密码输入不一致！</div>";
					$(".password1_tishi").html(errMsg);
					return false;
				}else{
					var Msg = "<div class='true'>确认密码输入正确</div>";
					$(".password1_tishi").html(Msg);
					return true;
				}
			}
		}
		
		//验证个人姓名
		if($(this).is(".name")){
			if($(".name").val()==""){
				var errMsg = "<div class='true'>姓名不能为空！</div>";
				$(".name_tishi").html(errMsg);
				return false;
			}else{
				if(!$(".name").val().match(/^[\u4e00-\u9fa5]{2,4}$/i)){
					var errMsg = "<div class='true'>姓名格式不正确！</div>";
					$(".name_tishi").html(errMsg);
					return false;
				}else{
					var Msg = "<div class='true'>>姓名输入正确！</div>";
					$(".name_tishi").html(Msg);
					return true;				
				}
			}
		}
		
		//验证联系电话
		if($(this).is(".telephone")){			
			var phone=$(".telephone").val();
			if($(".telephone").val()==""){
				var errMsg = "<div class='true'>电话不能为空！</div>";
				$(".telephone_tishi").html(errMsg);
				return false;
			}else{
				if(!Mobile(phone)&&!Phone(phone)){
					var errMsg = "<div class='true'>输入电话不合法！</div>";
					$(".telephone_tishi").html(errMsg);
					return false;
				}else{
					var Msg = "<div class='true'>>电话可以使用！</div>";
					$(".telephone_tishi").html(Msg);
					return true;
				}
			}
		}	
		
		/////////////////////////////////////////////
		
		//验证银行账号
		if($(this).is(".bankNum")){			
			var banknum=$(".bankNum").val();
			if($(".bankNum").val()==""){
				var errMsg = "<div class='true'>银行账号不能为空！</div>";
				$(".bankNum_tishi").html(errMsg);
				return false;
			}else{
				if(!rcheckBankNo()){
					var errMsg = "<div class='true'>银行账号不合法！</div>";
					$(".bankNum_tishi").html(errMsg);
					return false;
				}else{
					var Msg = "<div class='true'>>银行账号可以使用！</div>";
					$(".bankNum_tishi").html(Msg);
					return true;
				}
			}
		}		
		
		//验证收款人姓名
		if($(this).is(".card_name")){
			var card_name=$("#bank_receiver_name").val();
			if(card_name==""){
				var errMsg = "<div class='true'>收款人姓名不能为空！</div>";
				$(".card_name_tishi").html(errMsg);
				return false;
			}else{
				if(!card_name.match(/^[\u4e00-\u9fa5]{2,4}$/i)){
					var errMsg = "<div class='true'>收款人姓名不合法！</div>";
					$(".card_name_tishi").html(errMsg);
					return false;
				}else{
					var Msg = "<div class='true'>>收款人姓名输入正确！</div>";
					$(".card_name_tishi").html(Msg);
					return true;
				}
			}
		}
		
		//验证身份证号
		if($(this).is(".card")){
			var card=$(".card").val();
			if($(".card").val()==""){
				var errMsg = "<div class='true'>身份证不能为空！</div>";
				$(".card_tishi").html(errMsg);
				return false;
			}else{
				if(!cardNum(card)){
					var errMsg = "<div class='true'>输入身份证不合法！</div>";
					$(".card_tishi").html(errMsg);
					return false;
				}else{
					var Msg = "<div class='true'>身份证输入正确！</div>";
					$(".card_tishi").html(Msg);
					return true;
				}
			}
		}
		
		//验证营业执照
		if($(this).is(".company_license")){
			var company_license=$(".company_license").val();
			if($(".company_license").val()==""){
				var errMsg = "<div class='true'>营业执照号不能为空！</div>";
				$(".company_license_tishi").html(errMsg);
				return false;
			}else{
				if(!rcheckLicense(company_license)){
					var errMsg = "<div class='true'>营业执照号不合法！</div>";
					$(".company_license_tishi").html(errMsg);
					return false;
				}else{
					var Msg = "<div class='true'>>营业执照号输入正确！</div>";
					$(".company_license_tishi").html(Msg);
					return true;
				}
			}
		}
		
		//验证码
		if($(this).is(".code")){
			if($("#verify").val()==""){
				var errMsg = "<div class='true'>验证码不能为空！</div>";
				$(".code_tishi").html(errMsg);
				return false;
			}
			else if(!rcheckVerify($.trim($("#verify").val())))
			{
				var errMsg = "<div class='true'>验证码错误！</div>";
				$(".code_tishi").html(errMsg);
			}
			else{
				var Msg = "<div class='true'>>验证码输入正确！</div>";
				$(".code_tishi").html(Msg);
			}
		}

	});
	
	if ($("#address").val() == '') $("#address").val("填写公司详细地址");	
	$(".address").focus(function(){
		if ($.trim($(".address").val()) == '填写公司详细地址')
		{
			$(this).val("");
		}
	})
	$(".address").blur(function(){
		if ($.trim($(".address").val()) == '')
		{
			$(this).val("填写公司详细地址");
		}
	});	
	$(".bank_address").focus(function(){
		if ($.trim($(".bank_address").val()) == '请写开户行')
		{
			$(this).val("");
		}
	})
	$(".bank_address").blur(function(){
		if ($.trim($(".bank_address").val()) == '')
		{
			$(this).val("请写开户行");
		}
	});	
	
});

function valid_email(email) {
	var patten = new RegExp(/^[\w-]+(\.[\w-]+)*@([\w-]+\.)+(com|cn)$/);
	return patten.test(email);
}

function Mobile(phone){
	var patten=/^(?:13\d|15\d)\d{5}(\d{3}|\*{3})$/;
	return patten.test(phone);
}
function Phone(phone){
	var patten=/^((0\d{2,3})-)?(\d{7,8})(-(\d{3,}))?$/;
	return patten.test(phone);
}
function cardNum(card){
	var patten = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;
	return patten.test(card);
}

function setCityList(p ,cid) {
	var jsonrtn = '';
	var co_id = arguments[2] ? arguments[2] : '';
	var selected = '';
	$.ajax({
		type:"POST",
		url:"./index.php?m=ajax&a=city",
		data:"p=" + p + '&r=' + Math.random(),
		async: false,
		success:function(rs) {
			jsonrtn = eval(rs);	
		}
	});		
	$('#'+ cid +' option:gt(0)').remove();
	for(var i=0; i<jsonrtn.length; i++) {
		if (co_id == jsonrtn[i].city_id){
			var selected = 'selected="selected"';
		}else{
			var selected = '';
		}		
		$('#'+cid).append("<option "+selected+" value='"+jsonrtn[i].city_id+"'>"+ jsonrtn[i].city_name +"</option>");
	}
}

function check_step1_form()
{
	var username = $('[name="username"]');
	var password = $('[name="password"]');
	var re_password = $('[name="re_password"]');
	var email = $('[name="email"]');
	var fullname = $('[name="fullname"]');
	var tel = $('[name="tel"]');
	var company = $('[name="company"]');
	var address = $('[name="address"]');
	var zip = $('[name="zip"]');
	var agreement = ($("input:checked").val() == "on") ? 1 : 0;	
	var flg = validate_step1_form(username, password, re_password, email, fullname, tel, agreement);
	if (flg)
	{
		$(".submit").attr("disabled","disabled");
		$.ajax({
			url: 'regHandle',
			type: "POST",
			dataType:'json',
			data: {
				username:$.trim(username.val()), 
				password:$.trim(password.val()), 
				repassword:$.trim(repassword.val()),
				email:$.trim(email.val()),
				fullname:$.trim(fullname.val()),
				tel:$.trim(tel.val()),
				address:$.trim(address.val()),
				zip:$.trim(zip.val())
			},
			success: function (data) {
				$(".submit").removeAttr("disabled");
				if (data.status == 1)
				{
					window.location.href = '';
				}
				else
				{				
					alert(data.info);
				}
			}			
		});
	}
	
}

function validate_step1_form(username, password, re_password, email, fullname, tel, agreement)
{
	if ($.trim(username.val()) == '')
	{	
		form_tips('用户名不能为空！','username_tishi', username);
		return false;
	}
	if (username.val().length <6)
	{	
		form_tips('用户名不能少于6位！','username_tishi', username);
		return false;
	}
	if ($.trim(password.val()) == '')
	{
		form_tips('密码不能为空！','password_tishi', password);
		return false;
	}
	if (password.val().length <6)
	{	
		form_tips('密码不能小于6位！','password_tishi', password);
		return false;
	}
	if ($.trim(password.val()) != $.trim(re_password.val()))
	{
		form_tips('密码输入不一致！','re_password', re_password);
		return false;
	}
	if ($.trim(email.val()) == '')
	{
		form_tips('邮箱不能为空！','emali_tishi', email);
		return false;
	}
	if (!valid_email(email.val()))
	{
		form_tips('邮箱不合法！','emali_tishi', email);
		return false;
	}
	if ($.trim(fullname.val()) == '')
	{
		form_tips('姓名不能为空！','name_tishi', fullname);
		return false;
	}
	if (!fullname.val().match(/^[\u4e00-\u9fa5]{2,4}$/i))
	{
		form_tips('姓名格式不正确！','name_tishi', fullname);
		return false;
	}
	if ($.trim(tel.val()) == '')
	{
		form_tips('电话不能为空！','telephone_tishi', tel);
		return false;
	}
	if (!Mobile(tel.val())&&!Phone(tel.val()))
	{
		form_tips('输入电话不合法！','telephone_tishi', tel);
		return false;
	}
	if (!agreement)
	{
		alert('请勾选并同意易瑞特合作条款！');
		return false;
	}

	return true;
}

function form_tips(msg, tip_countainer_class, dom)
{
	$("."+tip_countainer_class).html('');
	var error_html = "<div class='true'>" + msg + "</div>";
	$("."+tip_countainer_class).html(error_html);
	if (dom != '') dom.focus();
}

//用户名唯一检测
function rcheckUniquename() {
	var rtn ;
	$.ajax({
		type:"POST",
		url:"check",
		data:"username=" + $('#username').val() + '&r=' + Math.random(),
		async: false,
		success:function(data) {
			rtn = data;
		}
	});  
	if(rtn['info'] == 1) {
		return true;
	} else {
		return false;
	}
}

//邮箱地址唯一检测
function rcheckUniqueemail() {
	var rtn ;
	$.ajax({
		type:"POST",
		url:"check",
		data:"email=" + $('#email').val() + '&r=' + Math.random(),
		async: false,
		success:function(rs) {
			rtn = rs;
		}
	});
	if(rtn['info'] == 1) {
		return true;
	} else {
		return false;
	}
}


//检测银行帐号
function rcheckBankNo() {
	if($('#bank_no').val().length < 10) {
		return false;
	} else {
		return true;
	}
}

//检测付款银行/开户行
function rcheckBankWh() {
	if($('#bank_wh').val().length < 5 || $('#bank_id').val() == 0 || $('#bank_wh').val() == '请写开户行') {
		return false;
	} else {
		return true;
	}
}

//检测开户地
function rcheckBankProvince() {
	if($('#bank_province').val() == 0) {
		return false;
	} else {
		if(document.getElementById('bank_city').options.length !=1 && $('#bank_city').val() == 0) {
			return false;
		} else {
			return true;
		}
	}
}

//检测收款人姓名
function rcheckReceiver() {
	if($('#bank_receiver_name').val().length < 2) {
		return false;
	} else {
		return true;
	}
}

//检测收款人身份证号
function rcheckReceiverId() {
	if($('#bank_receiver_idcard').val().length == 15 || $('#bank_receiver_idcard').val().length == 18) {
		return true;
	} else {
		return false;
	}
}

//检测营业执照号
function rcheckLicense() {
	if($('#company_license').val().length < 10) {
		return false;
	} else {
		return true;
	}
}


//检测验证码
function rcheckVerify() {
	var rtn = checkVerify($('#verify').val());
	if(rtn == 1) {
		return true;
	} else {
		return false;
	}
}

//验证码
function rcheckVerify() {
	var rtn ;
	$.ajax({
		type:"POST",
		url:"./index.php?m=ajax&a=verify",
		data:"v=" + $('#verify').val() + '&r=' + Math.random(),
		async: false,
		success:function(rs) {
			rtn = rs;
		}
	});
	if(rtn == 1) {
		return true;
	} else {
		return false;
	}
}

//个人与企业切换
function changeFinance(val) {
	if(val == 1) {
		$('#personal').css('display','block');
		$('#enterprise').css('display','none');
	} else if(val == 2){
		$('#personal').css('display','none');
		$('#enterprise').css('display','block');
	}
	return false;
}


function check_step2_form()
{
	var finance_obj = $('[name="finance_obj"]:checked');
	var bank_no = $('[name="bank_no"]');
	var bank_id = $('[name="bank_id"]');
	var bank_wh = $('[name="bank_wh"]');
	var province =$('[name="province"]');
	var city =$('[name="city"]');
	var bank_receiver_name = $('[name="bank_receiver_name"]');
	var bank_receiver_idcard = $('[name="bank_receiver_idcard"]');
	var company_license = $('[name="company_license"]');
	var verify =$('[name="verify"]');
	var sid =$('[name="sid"]');	
	var flg = validate_step2_form(finance_obj, bank_no, bank_id, bank_wh, province, city, bank_receiver_name, bank_receiver_idcard, company_license, verify);
	
	if (flg)
	{
		$(".submit").attr("disabled","disabled");
		$.ajax({
			url: 'index.php?m=merchant&a=doFinance',
			type: "POST",
			dataType:'json',
			data: {
				finance_obj:$.trim(finance_obj.val()), 
				bank_no:$.trim(bank_no.val()), 
				bank_id:$.trim(bank_id.val()),
				bank_wh:$.trim(bank_wh.val()),
				province:$.trim(province.val()),
				city:$.trim(city.val()),
				bank_receiver_name:$.trim(bank_receiver_name.val()),
				bank_receiver_idcard:$.trim(bank_receiver_idcard.val()),
				company_license:$.trim(company_license.val()),
				verify:$.trim(verify.val()),
				sid:$.trim(sid.val())
			},
			success: function (data) {
				$(".submit").removeAttr("disabled");
				if (data.status == 1)
				{
					window.location.href = './index.php?m=merchant&a=finish&username='+data.data;
				}
				else 
				{				
					alert(data.info);
				}
			}			
		});
	}
}

function validate_step2_form(finance_obj, bank_no, bank_id, bank_wh, province, city, bank_receiver_name, bank_receiver_idcard, company_license, verify)
{
	if ($.trim(bank_no.val()) == '')
	{
		form_tips('银行账号不能为空！','bankNum_tishi', bank_no);
		return false;
	}
	else if(!rcheckBankNo())
	{
		form_tips('银行账号不合法！','bankNum_tishi', bank_no);
		return false;
	}
	else
	{
		var Msg = "<div class='true'>>银行账号可以使用！</div>";
		$(".bankNum_tishi").html(Msg);
	}
	
	if (!rcheckBankWh())
	{
		form_tips('请选择并填写开户行分行(5字以上)！','bankId_tishi', bank_id);
		return false;
	}
	else
	{
		$(".bankId_tishi").html('');
	}
	
	if ($.trim(province.val()) == '0')
	{
		form_tips('请选择开户地！','province_tishi', province);
		return false;
	}
	else
	{
		$(".province_tishi").html('');
	}
	
	// p
	if (finance_obj.val() == '1')
	{
		if ($.trim(bank_receiver_name.val()) == '')
		{
			form_tips('收款人姓名不能为空！','card_name_tishi', bank_receiver_name);
			return false;
		}
		else if (!bank_receiver_name.val().match(/^[\u4e00-\u9fa5]{2,4}$/i))
		{
			form_tips('收款人姓名不合法！','card_name_tishi', bank_receiver_name);
			return false;
		}
		else
		{
			var Msg = "<div class='true'>>收款人姓名输入正确！</div>";
			$(".card_name_tishi").html(Msg);
		}
		
		if ($.trim(bank_receiver_idcard.val()) == '')
		{
			form_tips('身份证不能为空！','card_tishi', bank_receiver_idcard);
			return false;
		}
		else if(!cardNum(bank_receiver_idcard.val()))
		{
			form_tips('输入身份证不合法！','card_tishi', bank_receiver_idcard);
			return false;
		}
		else
		{
			var Msg = "<div class='true'>>身份证输入正确！</div>";
			$(".card_tishi").html(Msg);
		}

	}
	// e
	if (finance_obj.val() == '2')
	{
		if ($.trim(company_license.val()) == '')
		{
			form_tips('营业执照号不能为空！','company_license_tishi', company_license);
			return false;
		}
		else if (!rcheckLicense())
		{
			form_tips('营业执照号不合法！','company_license_tishi', company_license);
			return false;
		}
		else
		{
			var Msg = "<div class='true'>>营业执照号输入正确！</div>";
			$(".company_license_tishi").html(Msg);
		}
	}

	if ($.trim(verify.val()) == '')
	{
		form_tips('验证码不能为空！','code_tishi', verify);
		return false;
	}
	else if(!rcheckVerify($.trim(verify.val())))
	{
		form_tips('验证码错误！','code_tishi', verify);
		return false;			
	}
	else
	{
		var Msg = "<div class='true'>>验证码输入正确！</div>";
		$(".code_tishi").html(Msg);
	}

	return true;
}